using Headspring.Training.Domain.Model;
using Headspring.Training.Domain.Model.StateCommands;
using Headspring.Training.Domain.Services;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;

namespace Headspring.Training.Domain.UnitTests.Model.StateCommands
{
    [TestFixture]
    public class AssignedToCancelledCommandTester
    {
        [Test]
        public void ShouldNotBeValidInWrongStatus()
        {
            WorkOrder order = new WorkOrder();
            order.Status = WorkOrderStatus.Complete;
            Employee employee = new Employee();
            order.Assignee = employee;

            AssignedToCancelledCommand command = new AssignedToCancelledCommand(order, employee);
            Assert.That(command.IsValid(), Is.False);
        }

        [Test]
        public void ShouldNotBeValidWithWrongEmployee()
        {
            WorkOrder order = new WorkOrder();
            order.Status = WorkOrderStatus.Assigned;
            Employee employee = new Employee();
            order.Assignee = employee;

            AssignedToCancelledCommand command = new AssignedToCancelledCommand(order, new Employee());
            Assert.That(command.IsValid(), Is.False);
        }

        [Test]
        public void ShouldBeValid()
        {
            WorkOrder order = new WorkOrder();
            order.Status = WorkOrderStatus.Assigned;
            Employee employee = new Employee();
            order.Creator = employee;

            AssignedToCancelledCommand command = new AssignedToCancelledCommand(order, employee);
            Assert.That(command.IsValid(), Is.True);
        }

        [Test]
        public void ShouldTransitionStateProperly()
        {
            WorkOrder order = new WorkOrder();
            order.Number = "123";
            order.Status = WorkOrderStatus.Assigned;
            Employee employee = new Employee();
            order.Assignee = employee;

            MockRepository mocks = new MockRepository();
            IWorkflowCoordinator coordinator = mocks.CreateMock<IWorkflowCoordinator>();
            coordinator.SaveWorkOrder(order);
            coordinator.SendMessage("You have cancelled work order 123");
            coordinator.EditWorkOrder(order);
            mocks.ReplayAll();

            AssignedToCancelledCommand command = new AssignedToCancelledCommand(order, employee);
            command.Execute(coordinator);

            mocks.VerifyAll();
            Assert.That(order.Status, Is.EqualTo(WorkOrderStatus.Cancelled));
        }
    }
}